/*
 * @Author: liu
 * @Date: 2024-05-22 00:09:25
 * @LastEditors: liu
 * @LastEditTime: 2024-06-13 01:09:15
 * @FilePath: \yyx_admin\src\api\upload\ali.ts
 * @Description:
 * 天道酬勤
 */
import request from '@/utils/request'
import { ElMessage } from 'element-plus'
//import { lessonInfoData } from '../course/type'
import { ImageAuth, TransCode, videoAuth, vodForm } from './type'

enum API {
  //获取阿里云视频上传凭证接口
  UPLOADAUTH_URL = '/yyx/uploadAuth',
  UPLOADIMGAUTH_URL = '/yyx/uploadImgAuth',
  TRANSCODE_URL = '/yyx/transCode',
  GETTRANSCODETASK = '/yyx/transCodeTask',
  //更新章节信息接口
  UPDATELESSON_URL = '/yyx/course/saveOrUpdateLesson'
}

type Common<T> = {
  code: number
  msg: string
  data: T
}

//1、获取视频上传凭证
export const reqUploadAuth = async (fileName: string, imageUrl: string) => {
  try {
    const res = await request.get<Common<videoAuth>>(
      API.UPLOADAUTH_URL + `/?fileName=${fileName}&imageUrl=${imageUrl}`
    )
    return res
  } catch (err: any) {
    console.log(err)
    ElMessage({
      type: 'error',
      message: err.msg
    })
  }
}

//2、获取图片上传凭证
export const reqUploadImgAuth = async (imageType: string) => {
  try {
    const res = await request.get<Common<ImageAuth>>(
      API.UPLOADIMGAUTH_URL + `/?imageType=${imageType}`
    )
    return res
  } catch (err) {
    ElMessage({
      type: 'error',
      message: '获取图片上传凭证失败!'
    })
  }
}

//3、视频转码处理
export const transCode = async (transCode: Partial<TransCode>) => {
  // console.log(transCode)
  try {
    return request<Common<boolean>>({
      method: 'POST',
      url: API.TRANSCODE_URL,
      data: transCode
    })
  } catch (err) {
    ElMessage({
      type: 'error',
      message: '视频转码失败!'
    })
  }
}

//3、获取转码任务详情(进度，详情等)
export const getTranscodeTask = async (transcodeTaskId: string) => {
  console.log(transCode)
  return request<Common<number>>({
    method: 'GET',
    url: API.GETTRANSCODETASK,
    params: {
      transcodeTaskId
    }
  })
}

//4、更新课程信息（上传视频部分）
export const updateLesson = (videoData: Partial<vodForm>) => {
  return request<any, any>({
    method: 'POST',
    url: API.UPDATELESSON_URL,
    data: videoData
  })
}

export const test = async (transcodeTaskId: string) => {
  return request<Common<number>>({
    method: 'GET',
    url: API.GETTRANSCODETASK,
    params: {
      transcodeTaskId
    }
  })
}
